<?php
class UsersController extends AppController {
	var $name = 'Users';
	var $helpers = array("Html","Session");
	var $components = array("Session", "Email");
	//var $helpers = array('Html','Form','Javascript','Session','Utils');
	var $uses = array('User');
	function beforeFilter()
	{
		parent::beforeFilter();
	}
	
	//ThanhBC: xu ly phan dang nhap
	// function login()
	// {
		// $this->layout = 'backend/profiadmin_login';
		// if (!empty($this->data))		//lay du lieu tu form login
		// {			
			// $results = $this->User->findByUsername($this->data['User']['username']);
			// if ($results && $results['User']['password'] == md5($this->data['User']['passwd']))
			// {
				// $this->Session->setFlash('Xin chao ban');
				// $this->Session->write('username', $this->data['User']['username']);
				// $this->redirect(array('controller'=>'Users', 'action'=>'index'));
			// }
			// else
			// {
				// $this->Session->setFlash('User or password is not correct! Please try again!');
			// }
		// }	
	// }
	function login() {
		$this->layout = 'backend/profiadmin_login';
		if(!empty($this->data)) {
			pr($this->Auth->password('admin'));
		}
		$this->render('admin_login');
	}
	function logout()
	{
		$this->Auth->logout();
		$this->Session->delete('Message.auth');
		$this->redirect('/users/login', null, true);
	}

	function admin_logout()
	{
		$this->logout();
	}
	
	//ThanhBC: register a new user
	function register()
	{
		if (!empty($this->data))
		{
			$this->User->create();
			//ma hoa md5 cho password
			$this->data['User']['password'] = md5($this->data['User']['password']);
			if ($this->User->save($this->data))
			{
				$this->Session->setFlash('Luu thanh cong!');
				$this->Session->write('username', $this->data['User']['username']);
				$this->redirect(array('controller'=>'Users', 'action'=>'index'));
			}
			else
			{
				$this->Session->setFlash('Co loi trong viec luu du lieu!');
			}
		}
	}
	
	//ThanhBC: find knownusers
	function knownusers()
	{
		$this->set('knownusers', $this->User->find('all',
			array(
					'fields'=>array('id', 'username', 'firstname', 'lastname', 'email', 'phone'),
					'order'=>'id DESC'
				)
			));
	}

	
	function findfirst()
	{
		$this->set('aaa',		$this->User->find('first'));
	}
	
	function findlist()
	{
		$this->set('listusers', $this->User->find('list'));
	}
	//pV rem
/*	
	function logout()
	{
			//xoa bo session
			$this->Session->delete('username');
			$this->redirect(array('controller'=>'Users', 'action'=>'login'));//tu dong chuyen trang sau khi logout
	}
*/	
/*
	function index() {
		//check if logged in or not?
		if ($this->Session->check('username'))
		{
			$usr_tmp = $this->Session->read('username');
			$this->User->recursive = 0;
			$this->set('users', $this->paginate());
			$this->set('user_tmp', $usr_tmp);		//bien de dua ra view
		}
		else		//return back login form
		{
			$this->redirect(array('controller'=>'Users', 'action'=>'login'));
		}
	}
*/
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}